/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managers;

import entity.Client;
import entity.Langue;
import entity.Utilisateur;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.sql.Date;
import java.util.GregorianCalendar;
import java.util.Random;
import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import mail.CreateMail;
import session.ClientFacade;
import session.LangueFacade;
import session.PaysClientFacade;
import session.TypeClientFacade;
import session.UtilisateurFacade;

/**
 *
 * @author livetnediser
 */

@Stateless
public class UserManager {

    @PersistenceContext(unitName = "SexyShopPU")
    private EntityManager em;
    
    @EJB
    private PaysClientFacade paysClientFacade;
    @EJB
    private LangueFacade langueFacade;
    @EJB
    private TypeClientFacade typeClientFacade;
    @EJB
    private UtilisateurFacade utilisateurFacade;
    @EJB
    private ClientFacade clientFacade;

    public Client createCustomer(String nom, String prenom, String mail, String phone, String address, String city, String adressecomp, String url, boolean admin, int langue, int pays, int jour, int mois, int annee) {
	GregorianCalendar cal = new GregorianCalendar();
        cal.set(annee, mois, jour);
        Client c = new Client();
        c.setNom(nom+"#"+prenom);
	c.setAdresse(address+"#"+city);
        c.setAdresseComp(adressecomp);
        c.setEmail(mail);
        c.setTelephone(phone);
        c.setDateNaissance(new Date(cal.getTimeInMillis()));
        c.setUrl(url);
        if(admin){
            c.setIdType(1);
        }else{
            c.setIdType(2);
        }
        c.setIdLangue(langueFacade.find(langue));
        c.setIdPays(paysClientFacade.find(pays));
        em.persist(c);
	return c;
    }
    
    public Utilisateur createUtilisateur(String login, String pass, Client idClient){
        em.flush();

        Utilisateur u = new Utilisateur();
        
        u.setLogin(login);
        u.setPassword(UserManager.md5(pass));
        u.setIdClient(idClient);
        
        em.persist(u);
        return u;
    }
    
    public Client updateCustomer(Client c, String nom, String prenom, String mail, String phone, String address, String city, String adressecomp, String url, int langue, int pays, int jour, int mois, int annee) {
	GregorianCalendar cal = new GregorianCalendar();
        cal.set(annee, mois, jour);
        c.setNom(nom+"#"+prenom);
	c.setAdresse(city+"#"+address);
        c.setAdresseComp(adressecomp);
        c.setEmail(mail);
        c.setTelephone(phone);
        c.setDateNaissance(new Date(cal.getTimeInMillis()));
        c.setUrl(url);
        c.setIdLangue(langueFacade.find(langue));
        c.setIdPays(paysClientFacade.find(pays));
        em.merge(c);
	return c;
    }
    
    public void updatePassword(String user, String pass){
        Utilisateur u = utilisateurFacade.findByUser(user);
        u.setPassword(UserManager.md5(pass));
        em.merge(u);
    }
 
    public static String md5(String s) {

        String r = null;
        try {
            if (s != null) {

                MessageDigest algorithm = MessageDigest.getInstance("MD5");
                algorithm.reset();
                algorithm.update(s.getBytes());
                byte bytes[] = algorithm.digest();
                StringBuffer sb = new StringBuffer();
                for (int i = 0; i < bytes.length; i++) {
                    String hex = Integer.toHexString(0xff & bytes[i]);
                    if (hex.length() == 1) {
                        sb.append('0');
                    }
                    sb.append(hex);
                }

                r = sb.toString();
            }
        } catch (NoSuchAlgorithmException e) {
        }

        return r;

    }
    
    public void changeType(String id, int type){
       Client c = clientFacade.find(Integer.parseInt(id));
       c.setIdType(type);
       em.merge(c);
    }
    
    public boolean newPass(String nom, String mail, Langue langue){
        
        GregorianCalendar g = new GregorianCalendar();
        Random r = new Random(g.getTimeInMillis());
        
        Utilisateur u = utilisateurFacade.findByUser(nom);
        
        if(u == null) return false;
        
        if(u.getIdClient().getEmail() != null && u.getIdClient().getEmail().equalsIgnoreCase(mail)){
            String pass = ""+r.nextInt(9999999);
            this.updatePassword(nom, pass);
            CreateMail c = new CreateMail();
            c.sendPass(pass, mail, langue.getIdlangue());
            return true;
        }
        
        return false;
    }
}
